package Aleehot100;

/**
 * ClassName: c092
 * Package: Aleehot100
 * Description:64. 最小路径和
 *
 * @Author BCXJ
 * @Create 2025/9/15 08:46
 * @Version 1.0
 * @Since 1.0
 */
public class c092 {
    public static void main(String[] args) {
        int[][] arr = {{1,3,1}
                ,{1,5,1},
                {4,2,1}};
        System.out.println(minPathSum(arr));
    }

    public static int minPathSum(int[][] arr) {
        int r = arr.length;
        int c = arr[0].length;
        int[][] dp = new int[r][c];
        // 初始化行、列
        dp[0][0] = arr[0][0];
        for (int i = 1; i < c; i++) {
            dp[0][i] = dp[0][i - 1] + arr[0][i];
        }
        for (int i = 1; i < r; i++) {
            dp[i][0] = dp[i - 1][0] + arr[i][0];
        }
        // dp
        for (int i = 1; i < r; i++) {
            for (int j = 1; j < c; j++) {
                dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + arr[i][j];
            }
        }
        return dp[r - 1][c - 1];
    }
}
